<?php


namespace common\models\base;


use yii\db\ActiveRecord;

class BaseModel extends ActiveRecord
{
    /**
     * 获取分页数据
     * @param $query
     * @param int $curPage
     * @param int $pageSize
     * @param null $search
     * @return array
     */
    public function getPages($query, $curPage = 1, $pageSize = 10, $search = null)
    {
        $data = [];
        if ($search) {
            $query = $query->andFilerWhere($search);

        }
        $data['count'] = $query->count();
        if (!$data['count']) {
            return ['count' => 0, 'curPage' => $curPage, 'pageSize' => $pageSize, 'start' => 0, 'end' => 0, 'data' => []];
        }
        $curPage = ceil($data['count'] / $pageSize) < $curPage ? ceil($data['count'] / $pageSize) < $curPage : $curPage;
        $data['pageSize'] = $pageSize;
        $data['start'] = ($curPage - 1) * $pageSize + 1;
        $data['end'] = ceil($data['count'] / $pageSize) == $curPage ? $data['count'] : $curPage * $pageSize;
        $data['data'] = $query->offset(($curPage - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();
        return $data;

    }






}